# brick.py
import pygame
from pygame import Vector2
from pygame.sprite import Sprite
import random
from config import Config

class Brick(Sprite):
    def __init__(self, x, y, color):
        super().__init__()
        self.width = 70
        self.height = 20
        self.image = pygame.Surface((self.width, self.height))
        self.image.fill(color)
        self.rect = self.image.get_rect(topleft=(x, y))
        self.health = 1


class GeneticBrick(Brick):
    def __init__(self, x, y):
        super().__init__(x, y, (255, 0, 0))
        self.genetic_code = {
            'move_freq': random.randint(20, 40),  # 移动频率
            'move_speed': random.uniform(1, 3),  # 移动速度
            'direction': random.choice([-1, 1])  # 初始方向
        }
        self.config = Config()
        self.move_counter = 0

    def update(self):
        self.move_counter += 1
        if self.move_counter % self.genetic_code['move_freq'] == 0:
            self.rect.x += self.genetic_code['direction'] * self.genetic_code['move_speed']

            # 限制移动范围（使用 config 中的屏幕宽度）
            if self.rect.right > self.config.screen_width or self.rect.left < 0:
                self.genetic_code['direction'] *= -1
                self.rect.clamp_ip(pygame.Rect(
                    0, 50,
                    self.config.screen_width,
                    self.config.screen_height - 100  # 限制垂直范围
                ))

# 鸟群算法优化
class FlockingBrick(Brick):
    def __init__(self, x, y):
        super().__init__(x, y, (0, 0, 255))
        self.velocity = Vector2(random.uniform(-1, 1), random.uniform(-0.5, 0.5))
        #self.velocity = Vector2(random.uniform(-3, 3), random.uniform(-1, 1))
        self.max_speed = 20
        self.perception = 100  # 感知半径 # 80
        self.config = Config()

    def update(self, bricks):
        # 分离、对齐、聚合参数
        separation_force = Vector2(0, 0)
        alignment_force = Vector2(0, 0)
        cohesion_force = Vector2(0, 0)

        # 防碰撞参数
        avoidance_force = Vector2(0, 0)
        total = 0

        for brick in bricks:
            if brick == self: continue

            distance = self.rect.center.distance_to(brick.rect.center)
            if distance < self.perception:
                # 分离计算
                diff = Vector2(self.rect.center) - Vector2(brick.rect.center)
                separation_force += diff.normalize() / distance if distance > 0 else Vector2(0, 0)

                # 对齐计算
                alignment_force += brick.velocity

                # 聚合计算
                cohesion_force += Vector2(brick.rect.center)

                # 防碰撞强制分离
                if distance < 30:  # 最小安全距离
                    avoidance_force += diff.normalize() * (30 - distance)

                total += 1

        if total > 0:
            # 计算合力
            separation_force *= 1.5  # 分离权重
            alignment_force = (alignment_force / total).normalize() * 0.5
            cohesion_force = (cohesion_force / total - Vector2(self.rect.center)) * 0.1
            avoidance_force *= 2.0  # 防碰撞权重

            # 应用所有力
            self.velocity += separation_force + alignment_force + cohesion_force + avoidance_force

        # 速度限制
        if self.velocity.length() > self.max_speed:
            self.velocity.scale_to_length(self.max_speed)

        # 更新位置
        self.rect.center += self.velocity

        # 边界反弹
        if self.rect.left < 0 or self.rect.right > self.config.screen_width:
            self.velocity.x *= -1
        if self.rect.top < 50 or self.rect.bottom > self.config.screen_height - 100:
            self.velocity.y *= -1